/*var result =sessionStorage.getItem("resufflt")
var resultdsf =JSON.parse(result)
console.log(resultdsf)*/
var th={}
intuy();

var spuDataList=[];
function intuy() {
    $.ajax({
        /*root.inter.findProductInfoList*/
        /*"http://192.168.65.118:7050/aproduct/product/findProductInfoList"*/
        url:root.inter.findProductInfoList,
        contentType: "application/json;charset=UTF-8",
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify({

            "nameParam":sessionStorage.getItem("spuName"),
            "code":sessionStorage.getItem("Code"),
            "aliasName":sessionStorage.getItem("skuName"),//商品别名
            "helpcode":sessionStorage.getItem("helpCode"),//助记码
            "spuCode":sessionStorage.getItem("spuCode")
        }),
        success: function (result) {
            var handStr = '';

            if(result.data && result.data.length>0){
                $.each(result.data,function(k,v){
                    spuDataList.push(v);
                    spuDataList[k][v.spu_code]=[];
                    handStr+='<li class="pd-10" style="border-bottom: 1px dotted #dedede">'+
                        '<div>' +
                        '<span>'+v.spu_code+'</span>' +
                        '<span class="ml-80">品牌：</span><span class="pl-10">'+v.brand_name+'</span>' +
                        '<span class="ml-80">售卖信息：</span><span class="pl-10">'+v.area_name+'</span>'+
                        '<span class="ml-80 hidden">'+v.ongCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.brand_id+'</span>'+
                        '<span class="ml-80 hidden">'+v.towCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.threeCategoryId+'</span>'+
                        '<span class="ml-80 hidden">'+v.skuIds+'</span>'+
                        '<span class="ml-80 hidden">'+v.threeCategoryName+'</span>'+
                        '<span class="ml-80 hidden">'+v.sale_area_id+'</span>'+
                        '<span class="ml-80 hidden">'+v.ongCategoryName+'</span>'+
                        '<span class="ml-80 hidden">'+v.towCategoryName+'</span>'+
                        ' </div>'+
                        '<p class="mt-10">'+v.name+'</p>'+
                        '<div>'+
                        '<span>商品数：</span><span class="ml-10">'+v.sku_number+'</span>'+
                        '<span class="ml-80">已选数量：</span><span class="select_num ml-10"></span>'+
                        '<span class="f-r" onclick="spugoodss(this,'+k+','+v.spu_code+')">>></span>'+
                        '</div>'+
                        '</li>'
                });
            }
            $(".goods_spu").html(handStr)

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}
var picjh=""
var skuValueCode = [];
var spuValueCode = [];

var spuPriceList = [];
function spugoodss(el,key,spc) {
    //保存上一个spu的选中项
    //spuDataList
    var chooseListSkc = [];
    console.log('--------')
    console.log(spuDataList);
    console.log(spuDataList[key][spc]);
    if(spuDataList[key][spc].length>0){
        for(i=0;i<spuDataList[key][spc].length;i++){
            chooseListSkc.push(spuDataList[key][spc][i].sku_code);
        }
    }
    console.log(chooseListSkc);
    console.log('--------')
    if(spuPriceList.length>0){
        var ind = $('#spugoods').attr("data-ind");
        var spuCode = $('#spugoods').attr("data-spucode");
        spuDataList[ind][spuCode]=[];
        $('#spugoods').find(".radius").each(function (k,v) {
            if($(this).hasClass(('btn-primary'))){
                spuDataList[ind][spuCode].push(spuPriceList[k]);
            }
        })
    }



    th = el;
    $("#spugoods").empty();
    var skuIds=th.parentElement.previousElementSibling.previousElementSibling.children[9].innerHTML
    var sale_area_id=th.parentElement.previousElementSibling.previousElementSibling.children[11].innerHTML
    var spucodes = el.parentElement.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;
    $.ajax({
        /*root.inter.findSkuDatas*/
        url: root.inter.findSkuDatas,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        async:false,
        data: {
            "skuIds": skuIds,
            "sale_area_id":sale_area_id

        },
        success: function (result) {

            spuPriceList = result.data;
            $(".select_sku").css({
                display: "inline"
            });
            //获取spu信息赋值
            var oneCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[5].innerHTML
            var twoCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[7].innerHTML
            var threeCategoryId=th.parentElement.previousElementSibling.previousElementSibling.children[8].innerHTML
            var threeCategoryname=th.parentElement.previousElementSibling.previousElementSibling.children[10].innerHTML
            var brandId=th.parentElement.previousElementSibling.previousElementSibling.children[6].innerHTML
            var brand = th.parentElement.previousElementSibling.previousElementSibling.children[2].innerHTML;
            var spucodes = th.parentElement.previousElementSibling.previousElementSibling.firstElementChild.innerHTML;
            var productName = th.parentElement.previousElementSibling.innerHTML;
            var areaname=th.parentElement.previousElementSibling.previousElementSibling.children[4].innerHTML
            var ongCategoryName=th.parentElement.previousElementSibling.previousElementSibling.children[12].innerHTML;
            var towCategoryName=th.parentElement.previousElementSibling.previousElementSibling.children[13].innerHTML;
            var hangfhStr = '';
            if(result.data && result.data.length>0){
                $.each(result.data,function(k,v){
                    picjh=v.price
                    var areanames=""
                    var distributionprice=""
                    var prices=""
                        for(var i=0;i < v.price.length;i++){
                            console.log(v.price[i].area_name)
                            areanames=v.price[i].area_name
                            distributionprice=v.price[i].distribution_price
                            prices=v.price[i].price
                        }

                    var isChoose = '';
                    if(chooseListSkc.indexOf(v.sku_code) >=0 ){
                        isChoose=' btn-primary';
                    }

                    hangfhStr += '<input type="button" id=""  name="' + v.product_code + '" class="btn mr-10 mb-10 radius'+isChoose+'" value="' + v.specifications + '' + v.unit_name + '' + v.color + '">';

                });
            }
            $("#spugoods").attr('data-spucode',spc);
            $("#spugoods").attr('data-ind',key);
            $("#spugoods").html(hangfhStr);

            var count = 0;
           // $(function () {
                //获取sku
                $("#spugoods input").each(function (i) {
                    // console.log(count);
                    $(this).click(function () {
                        var oneSkuValueCode = {};
                        $(this).toggleClass("btn-primary");//添加颜色属性
                        if ($(this).hasClass("btn-primary")) {
                            console.log($(this).attr("id"));
                            count++;
                            oneSkuValueCode.attValue = $(this).val();
                            oneSkuValueCode.code = $(this).attr("id");
                            oneSkuValueCode.productcode = $(this).attr("name");
                            skuValueCode.push(oneSkuValueCode);
                            console.log(oneSkuValueCode)
                        } else {
                            count--;
                            for (var i = 0; i < skuValueCode.length; i++) {
                                if (skuValueCode[i].attValue == $(this).val()) {
                                    skuValueCode.remove(skuValueCode[i]);
                                }
                            }
                        }
                        el.previousElementSibling.innerHTML = count;//计算已选数量
                    });
                });

           // });


        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });

}


//确定
function resultfind() {
    if(spuPriceList.length>0){
        var ind = $('#spugoods').attr("data-ind");
        var spuCode = $('#spugoods').attr("data-spucode");
        spuDataList[ind][spuCode]=[];
        $('#spugoods').find(".radius").each(function (k,v) {
            if($(this).hasClass(('btn-primary'))){
                spuDataList[ind][spuCode].push(spuPriceList[k]);
            }
        })

    }


    if(skuValueCode ==""&& spuValueCode==""){
        $.Huimodalalert('请选择详细信息！',2000);
        return false;
    }
    var objArr = [];
    for(var i=0;i<spuDataList.length;i++){
        var v = spuDataList[i];
        var curSpc = v.spu_code;
        if(v[curSpc].length>0){
            for(var j=0;j<v[curSpc].length;j++){
                var curObj = {};
                var vv = v[curSpc][j];
                console.log(v)
                console.log(v.towCategoryName)
                curObj = {
                    "oneCategoryId":v.ongCategoryId,
                    "twoCategoryId":v.towCategoryId,
                    "threeCategoryId":v.threeCategoryId,
                    "categoryName":v.threeCategoryName,
                    "brand": v.brand_name,
                    "brandId":v.brand_id,
                    "modifyPriceWay": '40',
                    "saleAttriValue": vv.sale_attri_value,
                    "categoryDiscount": '0',
                    "flag": '10',
                    "customerId":sessionStorage.getItem("customerId"),
                    "customerCode":sessionStorage.getItem("customerCode"),
                    "createName": JSON.parse(decodeURIComponent(web.readCookie("loginData"))).user.name,
                    "spuCode": v.spu_code,
                    "productName":v.name,
                    "skuCode": vv.sku_code,
                    "helpCode":"",//助记码
                    "productCode":vv.product_code,//Code码
                    "imgUrl": vv.img_url,
                    "unitName": vv.unit_name,//计量单位
                    "areaName": v.area_name, //商品区域名
                    "prices":vv.price,
                    "contractNum":sessionStorage.getItem("contractNum"),
                    "oneCategoryName":v.ongCategoryName,
                    "twoCategoryName":v.towCategoryName

                };

                objArr.push(curObj);
            }
        }
    }


   console.log('canshu')
    console.log(objArr);

    $.ajax({
        contentType: "application/json;charset=UTF-8",
        /*"http://192.168.65.13:7030/acontract/cateDiscount/saveCategoryDiscount"*/
        /*root.inter.saveCategoryDiscount*/
        url:root.inter.saveCategoryDiscount,
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        data: JSON.stringify(objArr),
        success: function (data) {
            if(data.errorCode==0){
                // parent.location.reload(); // 父页面刷新
                window.parent.conter();
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
                $.Huimodalalert('修改成功！',2000);
            }else {
                $.Huimodalalert(data.msg,2000);
            }
        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    })
}



//取消
function guanss() {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
    layer.msg('已取消', {icon: 6,time:1000});
}

